//
//  ViewController.m
//  UIViewTransformPoint
//
//  Created by codew on 4/30/21.
//

#import "ViewController.h"


@interface ViewController ()

@property (nonatomic, strong) NSLock * lock;

@property (nonatomic, strong) NSRecursiveLock * recursiveLock;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // NSLock死锁演示============================================================
//    self.lock = [[NSLock alloc] init];
//    [self testlock1];


//    // 创建一个信号量
//    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
//    // wait操作, 主动的阻塞
//    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//
//    dispatch_semaphore_signal(semaphore);

    
    
    
    // 递归锁 ============================================================
//    self.recursiveLock = [[NSRecursiveLock alloc] init];
//    dispatch_queue_t queue = dispatch_queue_create("nidaye", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_async(queue, ^{
//
//        [self testrecursiveLock1];
//    });
//
//    dispatch_queue_t queue2 = dispatch_queue_create("nidaye2", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_async(queue2, ^{
//
//        [self testrecursiveLock1];
//    });
//
//    dispatch_queue_t queue3 = dispatch_queue_create("nidaye3", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_async(queue3, ^{
//
//        [self testrecursiveLock1];
//    });
    
    
    
    /**
     ================================================================================================================
     不能实现多个异步,回来后测试
     可以实现串行执行:
     */
//    [self semaphoreTest1];
    
    
}

//
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    NSLog(@"%s", __func__);
}



/**
 ================================================================================================================
 不能实现多个异步,回来后测试
 可以实现串行执行:
 */
- (void)semaphoreTest1
{
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // ================================================================================================================
    dispatch_async(quene, ^{
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL

                      URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmvideo/20210504/notificationThread01.mp4"];

        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            location;
            NSLog(@"%@, ok ok, 1", [NSThread currentThread]);
            dispatch_semaphore_signal(sem);

        }];
        
        [sessionDownloadTask resume];
    });
    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    
    // ================================================================================================================
    dispatch_async(quene, ^{
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL

                      URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmvideo/20210504/notificationThread01.mp4"];

        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            location;
            NSLog(@"%@, ok ok, 2", [NSThread currentThread]);
            dispatch_semaphore_signal(sem);

        }];


        [sessionDownloadTask resume];
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);


    // ================================================================================================================
    dispatch_async(quene, ^{
        
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL

                      URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmvideo/20210504/notificationThread01.mp4"];

        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            location;
            NSLog(@"%@, ok ok, 3", [NSThread currentThread]);
            dispatch_semaphore_signal(sem);

        }];

        [sessionDownloadTask resume];
        
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    

    // ================================================================================================================
    dispatch_async(quene, ^{
        
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL

                      URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmvideo/20210504/notificationThread01.mp4"];

        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            location;
            NSLog(@"%@, ok ok, 3+", [NSThread currentThread]);
            dispatch_semaphore_signal(sem);

        }];

        [sessionDownloadTask resume];
        
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    



    // ================================================================================================================
    dispatch_async(quene, ^{
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/images/videoPlaceholder.jpg"];

        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            location;
            NSLog(@"%@, ok ok, 4", [NSThread currentThread]);
            dispatch_semaphore_signal(sem);

        }];

        
        [sessionDownloadTask resume];
        
    });

    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    

}




// NSRecursiveLock 解决NSLock 死锁演示 ============================================================
- (void)testrecursiveLock1
{
    [self.recursiveLock lock];
    [self testrecursiveLock2];
    [self.recursiveLock unlock];

    NSLog(@"test1调用完成");
}

- (void)testrecursiveLock2
{
    [self.recursiveLock lock];
    NSLog(@"吃饭了没?");
    [self.recursiveLock unlock];
    NSLog(@"test2调用完成");
}


// NSLock死锁演示============================================================
- (void)testlock1
{
    [self.lock lock];
    [self testlock2];
    [self.lock unlock];
}

- (void)testlock2
{
    [self.lock lock];
    NSLog(@"吃饭了没?");
    [self.lock unlock];
}


@end
